<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

require_once("includes/news.class.php");

if($exec == 1){
	if(!isset($_GET['pd1'])){
		$cTheme->SetTitle("News");
		$id = 0;
		$data = $q->Get("SELECT news.id,link,text,title,time,username,users.id FROM news LEFT JOIN (users) ON (users.id=news.user) ORDER BY news.id DESC LIMIT 10", 60);
		foreach($data as $row){
			$id++;
			
			$new = new NewsParser($row['text']);
			
			$cTheme->AddModule(
				$row['title']." [<a href=\"{$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}news/{$row['link']}.html\">Read more</a>]",
				$new->Display(),
				10+$id,
				"Posted at {$row['time']} By: <a href=\"{$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}user/{$row['username']}.html\">{$row['username']}</a>"
			);
		}
		unset($id);
	}else {
		$data = $q->Get("SELECT news.id,link,text,title,time,username,users.id,allowvotes,allowcomments FROM news LEFT JOIN (users) ON (users.id=news.user) WHERE link=\"".$q->Escape($_GET['pd1'])."\"", 60);
		if(count($data) >= 1){
			if(isset($_SESSION['user'])){
				if(isset($_POST['commessage'])){
					$q->Send("INSERT INTO comments (userid, text, newsid) VALUES (\"{$_SESSION['user']['id']}\", \"".$q->Escape(htmlentities($_POST['commessage'],ENT_COMPAT,'utf-8'))."\", \"{$data[0]}\")");
					header("Location: {$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}".substr($_SERVER["REQUEST_URI"], strlen($_SESSION['sys']['core_link']))); // restrict f5 spamm
					exit;
				}
			}
			
			$new = new NewsParser($data['text'], true);
			
			$cTheme->SetTitle("{$data['title']}");
			$cTheme->AddModule(
				$data['title'],
				$new->Display(),
				10,
				"Posted at {$data['time']} By: <a href=\"{$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}user/{$data['username']}.html\">{$data['username']}</a>"
			);
			
			if($data['allowcomments']){
				$postData = $q->Get("SELECT comments.text,comments.time,users.username,users.settings FROM comments LEFT JOIN (users) ON (users.id=comments.userid) WHERE newsid={$data[0]} ORDER BY comments.id DESC", 30, true);
				if(count($postData) >= 1){
					$buffer = "<table>";
					foreach($postData as $row){
						//print_r($row);
						$av = new Profile($row['settings'], $row['username']);
						$buffer .= "<tr><td style=\"font-size:12px;\"><a href=\"{$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}user/{$row['username']}.html\">{$row['username']}</a></td><td>Posted at: {$row['time']}</td></tr><tr><td valign=\"top\">".$av->GetAvatar(true)."</td><td valign=\"top\">{$row['text']}</td></tr>";
					}
					$buffer .= "</table>";
				}else {
					$buffer = "No comments.";
				}
				$cTheme->AddModule("Comments", $buffer, 10);
				if(isset($_SESSION['user'])){
					$buffer = "<form method=\"post\">Message:<br /><textarea name=\"commessage\" rows=\"4\"></textarea><br /><input type=\"submit\" class=\"button\" value=\"Post\" style=\"border-top:0;\" /></form>";
					$cTheme->AddModule("Add comment", $buffer, 11);
				}
			}
		}else {
			$cTheme->AddModuleError("<b>404</b> This news article was not found on the server.
				<li><b>Article name</b>: {$_GET['pd1']}</lt>
			");
		}
	}
}else { header("Location: /"); } 